using System;
using System.Data;
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.ComponentModel; 
using LibreriaCommon; 

namespace LibreriaWCAjax
{
    /// <summary>
    /// Clase que extiende el control ComboBox 
    /// </summary>
    [ToolboxData("<{0}:ComboBoxBase runat=server></{0}:ComboBoxBase>")] 
    public class ComboBoxBase : ComboBoxAbstract 
    { 
		private wcCommon.DropDownListFirstItemEnum firstElement = wcCommon.DropDownListFirstItemEnum.Seleccione;
		private string itemValue = "0";
		private string customText = "CustomText";

        public ComboBoxBase() : base()
        {
        }

        /// <summary>
        /// Primer elemento a mostrar en la lista.
        /// </summary>
        [Category("Misc")] 
        public wcCommon.DropDownListFirstItemEnum FirstElement
        { 
            get 
            { 
				return firstElement;
            } 
            set 
            { 
                firstElement = value; 
            } 
        } 

		/// <summary>
		/// Valor asociado el primer elemento de la lista.
		/// </summary>
        [Category("Misc")] 
		public string ItemValue
		{ 
			get 
			{ 
				return itemValue; 
			} 
			set 
			{ 
				itemValue = value;
			} 
		} 
		
		/// <summary>
		/// Texto asociado al primer elemento de la lista.
		/// </summary>
        [Category("Misc")] 
		public string CustomText
		{ 
			get 
			{ 
				return customText; 
			} 
			set 
			{ 
				customText = value; 
			} 
		} 

        /// <summary>
        /// Llenar la lista con el primer elemento.
        /// </summary>
        public override void Fill() 
        { 
            if (firstElement != wcCommon.DropDownListFirstItemEnum.Ninguno)
            { 
				switch (firstElement)
				{
					case wcCommon.DropDownListFirstItemEnum.Seleccione:
						base.Items.Insert(0, new ListItem("- Seleccione -", ItemValue)); 
						break;

					case wcCommon.DropDownListFirstItemEnum.Todos:
						base.Items.Insert(0, new ListItem("- Todos -", ItemValue)); 
						break;

					case wcCommon.DropDownListFirstItemEnum.Vacio:
						base.Items.Insert(0, new ListItem("", ItemValue)); 
						break;

					case wcCommon.DropDownListFirstItemEnum.Custom:
						base.Items.Insert(0, new ListItem(CustomText, ItemValue)); 
						break;
				}
            } 

            Filled = true; 
        } 

		/// <summary>
		/// Obtener el valor seleccionado convertido en Int16 (short).
		/// </summary>
		/// <returns>valor convertido a Int16 (short).</returns>
        public short GetInt16()
		{ 
			try 
			{ 
				return Convert.ToInt16(SelectedValue); 
			} 
			catch (Exception ex) 
			{ 
				ErrorLog.Log(ex);
				throw ex;
			} 
		} 
		
        /// <summary>
        /// Obtener el valor seleccionado convertido en Int32 (int).
        /// </summary>
        /// <returns>valor convertido a Int32 (int).</returns>
        public int GetInt32() 
        { 
            try 
            { 
                return Convert.ToInt32(SelectedValue); 
            } 
            catch (Exception ex) 
            { 
				ErrorLog.Log(ex);
				throw ex;
			} 
        } 

        /// <summary>
        /// Obtener el valor seleccionado convertido en Int64 (long).
        /// </summary>
        /// <returns>valor convertido a Int64 (long).</returns>
        public long GetInt64() 
        { 
            try 
            { 
                return Convert.ToInt64(SelectedValue); 
            } 
            catch (Exception ex) 
            { 
				ErrorLog.Log(ex);
				throw ex;
			} 
        } 

        /// <summary>
        /// Ejecuta el DataBind e intenta mantener seleccionado el item actual.
        /// </summary>
        /// <param name="preserveSelectedItem">true o false.</param>
        public new void DataBind(bool preserveSelectedItem)
		{
			ListItem item = this.SelectedItem;
			
			this.DataBind();

			if (preserveSelectedItem && item != null)
			{
				this.SelectByValue(item.Value);
			}
		}

		/// <summary>
		/// DataBind.
		/// </summary>
        public override void DataBind() 
		{
			base.OnDataBinding(EventArgs.Empty);
			Fill();
		}

        /// <summary>
        /// Llena la lista con datos de un DataTable.
        /// </summary>
        /// <param name="dt">DataTable a utilizar.</param>
        /// <param name="dataTextField">Campo texto a utilizar.</param>
        /// <param name="dataValueField">Campo valor a utilizar</param>
        /// <returns>Refencia al Item seleccionado.</returns>
        public ListItem Fill(DataTable dt, string dataTextField, string dataValueField) 
        { 
            return Fill(dt, dataTextField, dataValueField, null); 
        } 

        /// <summary>
        /// Llena la lista con datos de un DataTable.
        /// </summary>
        /// <param name="dt">DataTable a utilizar.</param>
        /// <param name="dataTextField">Campo texto a utilizar.</param>
        /// <param name="dataValueField">Campo valor a utilizar</param>
        /// <param name="valor">Valor a seleccionar una vez llenada la lista.</param>
        /// <returns>Refencia al Item seleccionado.</returns>
        public ListItem Fill(DataTable dt, string dataTextField, string dataValueField, object valor) 
        { 
            try 
            { 
                Clear();

                if (dt.Rows.Count != 0)
                {
                    this.DataSource = dt.DefaultView;
                    this.DataValueField = dataValueField;
                    this.DataTextField = dataTextField;
                    this.DataBind();
                }
                else
                {
                    Fill();
                }
                
				if (valor != null) 
				{ 
					try 
					{ 
						this.SelectByValue(valor.ToString()); 
					} 
					catch {} 
				}
				else if (this.Items.Count > 0)
				{
					this.SelectedIndex = 0;
				}

                return this.SelectedItem;
            } 
            catch (Exception ex) 
            { 
                this.Items.Add(ex.Message); 
                return null;
            } 
        }

        /// <summary>
        /// Deshabilita el item especificado
        /// </summary>
        /// <param name="value">value del item a deshabilitar</param>
        public void DeshabilitarItem(int value)
        {
            base.Items.FindByValue(Convert.ToString(value)).Enabled = false;
        }

        /// <summary>
        /// Habilita el item especificado
        /// </summary>
        /// <param name="value">value del item a habilitar</param>
        public void HabilitarItem(int value)
        {
            base.Items.FindByValue(Convert.ToString(value)).Enabled = true;
        }
    }
}